// pages/publish/publish.js
import { Config } from '../../utils/config.js';
import { Home } from '../home/home-model.js';
var home = new Home(); //实例化 首页 对象
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cate: [],
    cate_id:'',
    sort:'',
    msg:'',
    imgList:[],
    address:'',
    name:'',
    gender:0,
    fee:10,
    feeType:0,
    tel:'',
    feeList:[],
    placeholder:'用户点击查看内容1000字以内',
    addImg:'../../images/publish/w3.png',
    flag:1,
    publish_btn:'',
    content:''
  },
  bindPickerChange: function (e) {
    var index = e.detail.value;
    var currentId = this.data.cate[index].id;
    var name = this.data.cate[index].name;
    this.setData({
      sort: name,
      cate_id:currentId
    })
  },
  showholder: function (e) {
    if (e.detail.value != '') {  //输入框不为空 span内容等于输入框内容
      this.setData({
        hiddenName: false,
        placeholder: e.detail.value,
        content:''
      })
    } else {
      this.setData({
        hiddenName: false,
        placeholder: '用户点击查看内容1000字以内',
        content: ''
      })
    }
  },
  tearerchange: function (e) {  //点击
    if (this.data.placeholder != '用户点击查看内容1000字以内') {
      this.setData({
        content: this.data.placeholder,
        hiddenName: true,
      })
    }
  },
  input:function(e){
    if (e.target.id == 'msg'){
      if (e.detail.value == '') {
        this.setData({
          content: '',
          hiddenName: true
        })
      } else {
        this.setData({
          hiddenName: true
        })
      }
    }
    var o={};
    o[e.target.id] = e.detail.value;
    this.setData(o);
  },
  radioChange: function (e) {
    var o = {};
    var id = (e.target.id == 'fee-box') ? 'feeType' : e.target.id;
    o[id] = e.detail.value;
    this.setData(o);
  },
  submit:function(e){
    var session_key = wx.getStorageSync('session_key');
    var user = wx.getStorageSync('user');
    var district_id = wx.getStorageSync('district_id');
    if (session_key == ''){
      wx.showToast({
        title: '没有登录',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    var type = this.data.feeType;
    var money = this.data.fee;
    if(type!='0'){
      money=this.data.feeList[type]['money'];
    }
    var data={
      "session_key":session_key,
      "cate_id": this.data.cate_id,
      "name": this.data.name,
      "sex": this.data.gender,
      "mobile": this.data.tel,
      "address": this.data.address,
      "img": this.data.imgList,
      "content": this.data.msg,
      "feeType": this.data.feeType,
      "money": money,
      'district_id': district_id
    }
    if (data.cate_id == '' || data.content == '' || data.name == '' || data.mobile == ''){
      wx.showToast({
        title: '信息填写不完整',
        icon: 'none',
        duration: 2000
      })  
      return false;
    }
    //console.log(data);
    //return false;
   if(this.data.flag){
     wx.showModal({
       title: '确认发布',
       content: '请点击确定',
       success: function (res) {
         if (res.confirm) {
           home.postSaveInfo(data, (res) => {
             if(res.status){
               wx.navigateTo({
                 url: '../../pages/pay/pay?money=' + money,
               })
             }else{
               wx.showToast({
                 title: res.info,
                 icon: 'none',
                 duration: 2000
               })  
             }
           });
         } 
       }
     })
   }else{
     home.postSaveInfo(data, (res) => {
       if (res.status) {
         wx.navigateTo({
           url: '../../pages/pay/pay?money=' + money,
         })
       } else {
         wx.showToast({
           title: res.info,
           icon: 'none',
           duration: 2000
         })
       }
     });
   }

  },

  upimg: function () {
    var that = this;
    if (this.data.imgList.length < 4) {
      wx.chooseImage({
        count: 4,
        sizeType: ['original', 'compressed'],
        success: function (res) {
        
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          if (tempFilePaths.length>4){
            wx.showToast({
              title: '最多上传四张图片',
              icon: 'loading',
              duration: 3000
            });
          }else{
            for (var i = 0; i < tempFilePaths.length; i++) {
                 
              home.upload(tempFilePaths[i], (url) => {
                that.setData({
                  imgList: that.data.imgList.concat(url)
                });

              });

            }
          }
        
        }
      })
    } else {
      wx.showToast({
        title: '最多上传四张图片',
        icon: 'loading',
        duration: 3000
      });
    }
  },    

  deleteImg:function(e){
    var index = e.target.id ? index : '';
    var imgList = this.data.imgList;
    var that=this;
    if (index != '') {
      wx.showModal({
        title: '删除图片',
        content: '请点击确定',
        success: function (res) {
          if (res.confirm) {
            imgList.splice(index, 1);
            that.setData({
              imgList: imgList
            });
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._loadData();
  },

  /*加载所有数据*/
  _loadData: function (callback) {
    var that = this;
    // 获得分类信息
    home.getCateData((data) => {
      that.setData({
        cate: data,
      });
    });

    home.getFeeTypeData((data) => {
      var publish_btn = Config.baseUrl + data.publish_btn;
      that.setData({
        feeList: data.items,
        flag: data.flag,
        publish_btn: publish_btn
      });
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})



